<?php

namespace App\Console\Commands\Cruise;

use App\Models\Common\ConfigModel;
use App\Models\Common\FsCompanyModel;
use App\Models\Common\FsCompanyRoleMappingModel;
use App\Models\Cruise\CruiseCheckDetailModel;
use App\Models\Cruise\CruiseTemplateModel;
use Illuminate\Console\Command;

class InitFsCompanyReviewer extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cruise:init-fs-company-reviewer';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '稽核-初始化三方公司的总部审核人';


    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $userIds = (array)json_decode(ConfigModel::query()
                                                 ->where('module', 'cruiseCheck')
                                                 ->where('name', 'KfAudit')
                                                 ->value('value'), true);

        FsCompanyModel::query()
                      ->where('organizationId', FsCompanyModel::WEB_ORGANIZATION_ID)
                      ->get()
                      ->each(function (FsCompanyModel $fsCompanyModel) use ($userIds) {

                          foreach ($userIds as $userId) {
                              FsCompanyRoleMappingModel::query()->updateOrCreate([
                                  'fsCompanyUserMappingId' => 0,
                                  'fsCompanyId'            => $fsCompanyModel->id,
                                  'rmsUserId'              => $userId,
                                  'roleId'                 => FsCompanyRoleMappingModel::ROLE_ID_KF_REVIEWER,
                              ], []);
                          }

                          $this->info($fsCompanyModel->id);
                      });
    }
}
